// autotest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "../UtilsLib/MemFile.h"
#include <memory>
#include <vector>
#include <iostream>
#include <conio.h>
#include "../LzmaLib/Types.h"
#include "../LzmaLib/LzmaLib.h"
#include "../Packer/PEStubWriter.h"
#include "MemFile.h"


const ULONG cbLoaderSize = 0x20000;
#define LZMA_DEF_RATIO 0;


typedef std::vector<unsigned char> UcharVt;


int _tmain(int argc, _TCHAR* argv[])
{
/*
	DeleteFile( L"E:\\WORK\\SimplePackPE\\test\\PackerPE\\Debug\\test.exe" );
	CopyFile( L"E:\\WORK\\SimplePackPE\\test\\PackerPE\\Release\\stub2rsrc.exe",
			  L"E:\\WORK\\SimplePackPE\\test\\PackerPE\\Debug\\test.exe", FALSE );
*/

    DeleteFile( L"D:\\Students\\test\\PackerPE\\Debug\\test.exe" );
    CopyFile( L"D:\\Students\\test\\PackerPE\\Release\\stub2rsrc.exe",
        L"D:\\Students\\test\\PackerPE\\Debug\\test.exe", FALSE );

	if ( argc != 2 )
	{
		std::cout << "usage: autotest.exe \"[PathToFile]\"";
	}

	try
	{
        PE::PEStubWriter StubWriter;

		PE::MemPEFile MemPEFile( argv[1], PE::MEMFILE_ACCESS::MA_ALL );

        if ( !MemPEFile.IsValidPEFile() )
        {
            std::cout << L"Not valid PE-file" << std::endl;
        }

        PIMAGE_SECTION_HEADER pNewHead = MemPEFile.AddPESection( ".sec0", StubWriter.GetRequiredSize(),
            IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ );

/*
        StubWriter.InsertStub( pNewHead->PointerToRawData + MemPEFile.GetMemPtr(), MemPEFile );
        StubWriter.SetNewEntryPoint( pNewHead->VirtualAddress + sizeof(STUB_DATA), MemPEFile );
*/
		std::cout << L"GoodJob" << std::endl;
	}
	catch ( std::runtime_error& )
	{
		std::cout << L"Error opening selected file" << std::endl;
		return 0;
	}
	catch ( std::logic_error& )
	{
		std::cout << L"Dev error occured. Check." << std::endl;
	}
/*
	try 	//Test for use LzmaLib may be here
	{
		UcharVt SrcArr;
		UcharVt DestArr;
		int CompressionLevel = LZMA_DEF_RATIO;

		unsigned propsSize = LZMA_PROPS_SIZE;
		unsigned destLen = SrcArr.size() + SrcArr.size() / 3 + 128;
		DestArr.resize(propsSize + destLen);

		int res = LzmaCompress(
			&DestArr[LZMA_PROPS_SIZE], &destLen,
			&SrcArr[0], SrcArr.size(),
			&DestArr[0], &propsSize,
			CompressionLevel, 0, -1, -1, -1, -1, -1);

		DestArr.resize(propsSize + destLen); 

		if ( ( res != SZ_OK ) ||
			( propsSize != LZMA_PROPS_SIZE ) )
		{
			return false;
		}
	}
	catch ( std::runtime_error& )
	{
		std::cout << L"LzmaCompress failed" << std::endl;
	}
*/
	return 0;
}

